using UnityEngine;
using QFramework;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class PistolBullet : BulletFathers,ICanReadModels
	{

        public Transform startPos;

        public Vector2 dics;
        public float moveSpeed;
        public float MaxBulletFlyTimes;
        private float RunTime = 0;
        private int hotIndex = 0;


        public override Transform StartPos => startPos;


        public override BoxCollider2D boxCollider => SelfBoxCollider2D;

        public override int HotIndex => hotIndex;

        public override float MaxBulletFlyTime => MaxBulletFlyTimes;


        private void Awake()
        {


            ReadModels();





            //rig = this.GetComponent<Rigidbody2D>();

            SelfBoxCollider2D.OnTriggerEnter2DEvent(collider =>
            {
                //Debug.Log(collider.gameObject.name);
                var hurtbox = collider.GetComponent<HitHurtBox>();
                if (hurtbox!=null)
                {
                    if (hurtbox.Owner.CompareTag("Enemy"))
                    {
                        //Debug.Log("子弹攻击到了enemy");
                        var enemys = hurtbox.Owner.GetComponent<EnemyFathers>();



                        if (enemys is IEnemy enemy)
                        {
                            //Debug.Log("进入伤害计算");
                            DamageSystem.SetDamage(DataManager.Instance.pistolModel.PistolDamage.Value*
                                DataManager.Instance.abilityUpModel.AbilityBulletDamage.Value, enemy);
                            // this.DestroyGameObjGracefully();
                            PoolManager.Instance.Push(this.gameObject);
                        }



                    }
                }

            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        private void Start()
        {
           
        }

        public override void BulletMoveDic(Vector2 dic)
        {
            base.BulletMoveDic(dic);
            dics = dic;
        }

        public override void SetBulletScale(float scale)
        {
            base.SetBulletScale(scale);
            //Debug.Log("设置子弹体积");
            this.transform.localScale = new Vector3(scale, scale, scale);
        }

        public override void SetBulletSpeed(float speed)
        {
            base.SetBulletSpeed(speed);
            moveSpeed = speed;
        }

        private void Update()
        {
            GetRunTime();

            transform.Translate(dics * Time.deltaTime * moveSpeed);
        }


        public override void Dis()
        {
            base.Dis();
            if ((this.transform.position - Player.Instance.transform.position).magnitude > 6)
            {
                Destroy(this.gameObject);
            }
        }


        private void OnEnable()
        {
            RunTime = 0;
        }

        private void OnDisable()
        {
            RunTime = 0;
        }

        public void ReadModels()
        {
           // this.R
        }

        public override void GetRunTime()
        {
            base.GetRunTime();
            RunTime += Time.deltaTime;
            if (RunTime>=MaxBulletFlyTime)
            {
                PoolManager.Instance.Push(this.gameObject);
            }
        }

        public override void SetPos(Transform pos)
        {
            base.SetPos(pos);
            startPos = pos;
        }
        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }

       
    }
}
